package com.taichuan.keysupport.panel;

import android.content.Context;
import android.view.Gravity;
import android.view.KeyEvent;
import android.widget.EditText;

import com.taichuan.keysupport.Key;
import com.taichuan.keysupport.R;
import com.taichuan.keysupport.keyset.KeySet;
import com.taichuan.keysupport.keyset.T16KeySet;


public class T16KeyBoardPanelManager extends PanelManager {
    private boolean xClick = false;

    public T16KeyBoardPanelManager(Context context) {
        super(context);
    }

    @Override
    public void setXDrawable(int drawableId, boolean xClick) {
        super.setXDrawable(drawableId, xClick);
        this.xClick = xClick;
    }

    @Override
    protected void showPopupWindowInView(EditText editText) {
        int[] location = new int[2];
        editText.getLocationOnScreen(location);
        System.out.println("------------" + location[0] + "/" + location[1] + "/" + editText.getHeight());
        float marginRight = mContext.getResources().getDimension(R.dimen.t16key_margin_right);
        float keyPanelWidth = mContext.getResources().getDimension(R.dimen.t16key_body);
        int screenWidth = editText.getRootView().getWidth();
        System.out.println("------------" + marginRight + "/" + keyPanelWidth + "/" + screenWidth);
        mPopupWindow.showAtLocation(editText.getRootView(), Gravity.NO_GRAVITY,
                screenWidth - (int) (marginRight + keyPanelWidth), location[1] + editText.getHeight() + 5);
    }

    @Override
    public int getPanelLayoutResource() {
        return R.layout.t16key_bord;
    }

    @Override
    public int getPanelItemResource() {
        return R.layout.t16key_text;
    }

    @Override
    public KeySet initKeySet() {
        return new T16KeySet(this);
    }

    @Override
    public void onKeyDown(int keyCode) {
        isSameKeyIndex = false;
        if (keyCode == Key.Convert.KEYCODE_XING) { // *
            mKeySet.performClickX();
        } else if (keyCode == Key.Convert.KEYCODE_JING) { // #
            mKeySet.performClickJ();
        } else if (keyCode == Key.Convert.KEYCODE_GLZHX) {
        } else if (keyCode == KeyEvent.KEYCODE_0 && isDispatchZero()) { // 0
            mKeySet.performClickZero();
        } else if (keyCode == Key.Convert.KEYCODE_DEL) { // del
            setEditText("-1");
            oldKeyIndex = -1;
        } else {
            this.keyIndex = getKeyIndex(keyCode);
            if (oldKeyIndex == this.keyIndex) {
                if (mEditText != null) {
                    mEditText.removeCallbacks(this);
                    isSameKeyIndex = true;
                    mEditText.postDelayed(this, 1500);
                }
            }
            mKeySet.performKey(this.keyIndex, isSameKeyIndex);
            oldKeyIndex = this.keyIndex;

        }
    }

    boolean isDispatchZero() {
        return !this.xClick;
    }


    @Override
    boolean isDelayInput() {
        return jIndex != 0;
    }
}
